<template>
  <div class="login-wrap">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="login-container">
        <h1 class="title">用户注册</h1>
        <el-form-item label="选择角色" prop="type"> 
          <el-radio-group v-model="ruleForm.type">
            <el-radio label="jiazhang">家长</el-radio>
            <el-radio label="caipan">裁判</el-radio>
            <el-radio label="duiwu">单位</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="username">
          <el-input type="text" v-model="ruleForm.username"  placeholder="用户账号" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input type="text" v-model="ruleForm.name"  placeholder="真实姓名" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" show-password v-model="ruleForm.password" placeholder="用户密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="passwordConfirm">
          <el-input type="password" show-password v-model="ruleForm.passwordConfirm" placeholder="用户密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" style="width: 100%;">注册</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>
 
<script>
import {register} from '@/api/login'
export default{
  name:'Login',
  data(){
    var checkPhone = (rule, value, callback) => {
			if (value === "") {
				callback(new Error("请输入绑定的手机号码"))
			} else if (!/^(1[1-9][0-9])\d{8}$/.test(value)) {
				callback(new Error("请输入正确的手机号码"))
			} else {
				callback()
			}
		};
    var validatePass = (rule, value, callback) => {
      if (value === '') {
          callback(new Error('请输入密码'));
      } else {
      if (this.ruleForm.confirmPwd !== '') {
        this.$refs.ruleForm.validateField('passwordConfirm');
      }
      callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: '',
        username: '',
        type: 'jiazhang',
        password:'',
        passwordConfirm:'',
        teamid:''
      },
      rules: {
        type: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        username: [
          { validator: checkPhone,required: true, message: '手机号码不正确', trigger: 'blur'}
        ],
        name: [
          { required: true, message: '正确姓名', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        passwordConfirm: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur', required: true }
        ]
      }
    }
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.ruleForm.type === 'duiwu') {
            let time = parseInt(new Date().getTime() / 1000)
            this.ruleForm.teamid = time
            // console.log('111',time)
          }
          register(this.ruleForm).then(res=> {
            if(res.code === 200) {
              this.$message({
                message: '注册成功即将跳转到登陆页面',
                type: 'success'
              });
              this.$router.push('/login')
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>
 
<style>
  	.login-wrap {
  		box-sizing: border-box;
  		width: 100%;
  		height: 100%;
  		padding-top: 10%;
  		/* background-color: #112346; */
  		background-repeat: no-repeat;
  		background-position: center right;
  		background-size: 100%;
  	}
 
  	.login-container {
  		border-radius: 10px;
  		margin: 0px auto;
  		width: 300px;
  		padding: 30px 35px 15px 35px;
  		background: #fff;
  		border: 1px solid #eaeaea;
  		text-align: left;
  		box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  	}
 
  	.title {
  		margin: 0px auto 20px auto;
  		text-align: center;
  		color: #505458;
  	}
</style>